#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
# read data

path = r'/home/yuanfeng/pr_homework/report_02_Titanic/data/train.csv'
data_train = pd.read_csv(path)

data_train['relative'] = data_train.SibSp + data_train.Parch  
data_train.relative.value_counts()


data_train.Embarked[data_train.Embarked.isnull()] = 'S'

embark_dummies = pd.get_dummies(data_train['Embarked']) 
data_train = data_train.join(embark_dummies) 
data_train.drop(['Embarked'], axis=1,inplace=True)

from sklearn.ensemble import RandomForestRegressor 
#choose training data to predict age 
age_df = data_train[['Age','Survived','Fare', 'Parch', 'SibSp', 'Pclass']] 
age_df_notnull = age_df.loc[(data_train['Age'].notnull())] 
age_df_isnull = age_df.loc[(data_train['Age'].isnull())] 
X = age_df_notnull.values[:,1:] 
Y = age_df_notnull.values[:,0] 
# use RandomForestRegression to train data 
RFR = RandomForestRegressor(n_estimators=1000, n_jobs=-1) 
RFR.fit(X,Y) 
predictAges = RFR.predict(age_df_isnull.values[:,1:]) 
data_train.loc[data_train['Age'].isnull(), ['Age']]= predictAges


Sex_dummies = pd.get_dummies(data_train['Sex']) 
data_train = data_train.join(Sex_dummies) 
data_train.drop(['Sex'], axis=1,inplace=True)


from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier

#choose training data to predict age 
data_df = data_train[['Survived','Age','Fare', 'Parch',\
                      'SibSp', 'Pclass','female','male',\
                      'S','Q','C']] 

X = data_df.values[:,1:]
Y = data_df.values[:,0].reshape(-1,1)
X = StandardScaler().fit_transform(X)#标准化

X_train, X_test, y_train, y_test = train_test_split(X,Y,test_size=0.3)

score1 = []
score2 = []
for i in range(1,40):
    for j in range(1,4):
        clf = MLPClassifier(hidden_layer_sizes=(i))  
        clf.fit(X_train,np.ravel(y_train))
        y_pred = clf.predict(X_test)
        c = clf.score(X_test,y_test)
        score1.append(c)
    c = sum(score1)/len(score1)
    score1 = []
    print(c)
    score2.append(c)
    
plt.figure()
plt.plot(range(1,40),score2)
plt.xlabel('n1')
plt.ylabel('score')
plt.show()


#clf = MLPClassifier(hidden_layer_sizes=(21,8))  
#clf.fit(X_train,np.ravel(y_train))
#y_pred = clf.predict(X_test)
#c = clf.score(X_test,y_test)
#print(c)














